﻿using System;
using System.Windows.Forms;
using Business;
using Business.Entity.Contabil;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Entity.Sped;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Forms.Contabil
{
    public partial class FormTransferencia : FormDefault, IForm
    {
        public FormTransferencia()
        {
            InitializeComponent();
            transferenciaBindingSource.DataSource = new Transferencia();
            var list = TransferenciaRepository.GetSession()
                .CreateCriteria<PlanoContaReferencial>().List();
            contaOrigemBindingSource.DataSource = list;
            contaDestinoBindingSource.DataSource = list;
            InitFocusControl = dataTransferenciaDateEdit;
        }

        public object Novo()
        {
            var tr = new Transferencia();
            tr.DataTransferencia = DateTime.Now;
            CurrentObject = tr;
            ShowDialog();
            if (tr.Id == 0)
            {
                return null;
            }
            return tr;
        }

        public void Alterar(object entity)
        {
            CurrentObject = entity;
            ShowDialog();
        }

        public object Save()
        {
            if (!Validate().HasError)
            {
                TransferenciaRepository.Save((Transferencia) CurrentObject);
            }
            return CurrentObject;
        }

        public void Update(object entity)
        {
            Alterar(entity);
        }

        public bool Delete(object entity)
        {
            CurrentObject = entity;
            TransferenciaRepository.Delete((Transferencia) entity);
            return true;
        }

        public object CurrentObject
        {
            get
            {
                transferenciaBindingSource.EndEdit();
                return transferenciaBindingSource.DataSource;
            }
            set
            {
                transferenciaBindingSource.DataSource = value;
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            return DataValidation.ValidateEntity(CurrentObject);
        }

        public void Reload()
        {
            
        }

        public void Cancel()
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        public Object New()
        {
            return Novo();
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }
    }
}